//
//  BookDetailViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 13-1-5.
//  Copyright (c) 2013年 MEETStudio. All rights reserved.
//

#import "BookDetailViewController.h"

static ASIHTTPConnection *duty_connect;

static ASIHTTPConnection *make_appointment;

@interface BookDetailViewController ()

@end

@implementation BookDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)dealloc{
    
    if (duty_connect != nil) {
        duty_connect.connectionDelegate = nil;
        [duty_connect release];
        duty_connect = nil;
    }
    
    if (make_appointment != nil) {
        make_appointment.connectionDelegate = nil;
        [make_appointment release];
        make_appointment = nil;
    }

    
    [dutyArray release];
    
    [super dealloc];
}


- (id)initWithDutyId:(NSString *)_dutyId{
    
    self = [super init];
    if (self) {
        dutyId = _dutyId;
    }
    return self;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadData];
    
}


- (void)loadData{
    
    NSString *urlString = [NSString stringWithFormat:@"%@/dutyDetail?Duty_ID=%@",UCENTERBASEURL,dutyId];
	NSURL *url = [NSURL URLWithString:urlString];
    
    if (duty_connect!= nil) {
        duty_connect.connectionDelegate = nil;
        [duty_connect release];
        duty_connect = nil;
    }
    
    duty_connect = [[ASIHTTPConnection alloc] initGETWithURL:url resultDict:YES isCache:YES];
    duty_connect.connectionDelegate = self;
    
    [duty_connect startAsynchronous];
    
}

- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    dutyArray = [[NSArray alloc]initWithArray:[dict objectForKey:@"detaillist"]];
    
    [myTableView reloadData];
    
    
}
- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    
}


#pragma mark tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return dutyArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MyIdentifier = @"forumCell";
    
    NSDictionary *dict = [dutyArray objectAtIndex:indexPath.row];
    
    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] init] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
	    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)];
        [titleLable setTextAlignment:NSTextAlignmentLeft];
        [titleLable setTextColor:[UIColor blackColor]];
        titleLable.backgroundColor = [UIColor clearColor];
        titleLable.tag = 100;
        [cell addSubview:titleLable];
        
        [titleLable release];
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(250, 10, 50, 30)];
        [button setBackgroundImage:[UIImage imageNamed:@"button2bg_friend1_iphone.png"] forState:UIControlStateNormal];
        [button setTitle:@"预约" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tag = indexPath.row;
        [button addTarget:self action:@selector(bookAction:) forControlEvents:UIControlEventTouchUpInside
         ];
        [cell addSubview:button];
        [button release];
        
        
        
    }
    
    
    
    NSString *string = [dict objectForKey:@"detail_Name"];
    UILabel *lable = (UILabel *)[cell viewWithTag:100];
    lable.text = string;
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}


- (IBAction)bookAction:(id)sender{
    
    UIButton *button = (UIButton *)sender;
    
    //test
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"预约成功！请您按照预约时间就诊。"
                                                   delegate:self
                                          cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.delegate = self;
    
    [alert show];
    [alert release];
    
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
